package com.cykj.pos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cykj.pos.domain.BizMerchantReceiver;
import com.cykj.pos.domain.dto.BizAccessTokenDTO;
import com.cykj.pos.domain.dto.BizAgentidAppkeyDTO;
import com.cykj.pos.domain.dto.BizMerchantReceiverDTO;
import com.cykj.pos.mapper.BizMerchantReceiverMapper;
import com.cykj.pos.service.IBizMerchantReceiverService;
import com.cykj.pos.util.SaasSignatureUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 商户下发收款者Service业务层处理
 *
 * @author xxl
 * @date 2022-03-24
 */
@Service
public class BizMerchantReceiverServiceImpl extends ServiceImpl<BizMerchantReceiverMapper, BizMerchantReceiver> implements IBizMerchantReceiverService {

    @Resource
    private   BizMerchantReceiverMapper bizMerchantReceiverMapper;

    @Override
    public List<BizMerchantReceiver> queryList(BizMerchantReceiver bizMerchantReceiver) {
        LambdaQueryWrapper<BizMerchantReceiver> lqw = Wrappers.lambdaQuery();
        if (bizMerchantReceiver.getUserId() != null){
            lqw.eq(BizMerchantReceiver::getUserId ,bizMerchantReceiver.getUserId());
        }
        if (bizMerchantReceiver.getMerchId() != null){
            lqw.eq(BizMerchantReceiver::getMerchId ,bizMerchantReceiver.getMerchId());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getMerchName())){
            lqw.eq(BizMerchantReceiver::getMerchName ,bizMerchantReceiver.getMerchName());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getMerchIdcard())){
            lqw.eq(BizMerchantReceiver::getMerchIdcard ,bizMerchantReceiver.getMerchIdcard());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getMerchBankMobile())){
            lqw.eq(BizMerchantReceiver::getMerchBankMobile ,bizMerchantReceiver.getMerchBankMobile());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getMerchBankCardno())){
            lqw.eq(BizMerchantReceiver::getMerchBankCardno ,bizMerchantReceiver.getMerchBankCardno());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getMerchIdcardPositive())){
            lqw.eq(BizMerchantReceiver::getMerchIdcardPositive ,bizMerchantReceiver.getMerchIdcardPositive());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getContractStatus())){
            lqw.eq(BizMerchantReceiver::getContractStatus ,bizMerchantReceiver.getContractStatus());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getErroMessage())){
            lqw.eq(BizMerchantReceiver::getErroMessage ,bizMerchantReceiver.getErroMessage());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getDealNo())){
            lqw.eq(BizMerchantReceiver::getDealNo ,bizMerchantReceiver.getDealNo());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getSerialNo())){
            lqw.eq(BizMerchantReceiver::getSerialNo ,bizMerchantReceiver.getSerialNo());
        }
        if (StringUtils.isNotBlank(bizMerchantReceiver.getMerchIdcardBack())){
            lqw.eq(BizMerchantReceiver::getMerchIdcardBack ,bizMerchantReceiver.getMerchIdcardBack());
        }
        return this.list(lqw);
    }

    @Override
    public BizMerchantReceiver selectBizMerchantReceiverByMerchId(BizMerchantReceiver bizMerchantReceiver) {
        return bizMerchantReceiverMapper.selectBizMerchantReceiverByMerchId(bizMerchantReceiver);
    }

    @Override
    public void updateBizMerchantReceiver(BizMerchantReceiver bizMerchantReceiver) {
        bizMerchantReceiverMapper.updateBizMerchantReceiver(bizMerchantReceiver);
    }

    @Override
    public Long selectUserId(String deal_no) {
        return bizMerchantReceiverMapper.selectUserId(deal_no);
    }

    @Override
    public BizMerchantReceiver selectBizMerchantReceiverByUserId(Long userId) {
        return bizMerchantReceiverMapper.selectBizMerchantReceiverByUserId(userId);
    }

    /*AccessToken  一小时*/
    public String acquireAccessToken(BizAgentidAppkeyDTO bizAgentidAppkeyDTO){
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /*当前时间减一小时*/
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.HOUR, 1);
        Date dt1=rightNow.getTime();
        /*判断当前时间是否在一段时间内*/
        BizAccessTokenDTO bizAccessToken= bizMerchantReceiverMapper.selectAcquireAccessToken();
        if (bizAccessToken.getNumber().equals("0")){
            String token= SaasSignatureUtil.getToken(bizAgentidAppkeyDTO);
            BizAccessTokenDTO bizAccessTokenDTO=new BizAccessTokenDTO();
            bizAccessTokenDTO.setAccessTokenTimeStart(format.format(date));
            bizAccessTokenDTO.setAccessTokenTimeEnd(sdf.format(dt1));
            bizAccessTokenDTO.setAccessToken(token);
            bizAccessTokenDTO.setId(1l);
            bizMerchantReceiverMapper.updateAcquireAccessToken(bizAccessTokenDTO);
            return token;
        }else {
            return bizAccessToken.getAccessToken();
        }
    }
    /* 获取提现的编码密钥*/
    public BizAgentidAppkeyDTO getAgentidAppkey(Long userId){
        Long deptId= bizMerchantReceiverMapper.selectSysDeptId(userId);
        BizAgentidAppkeyDTO bizAgentidAppkeyDTO= bizMerchantReceiverMapper.selectAgentidAppkey(userId,deptId);
        return bizAgentidAppkeyDTO;
    }

    @Override
    public Long intserReceiverService(BizMerchantReceiver bizMerchantReceiver) {
        return bizMerchantReceiverMapper.intserReceiverService(bizMerchantReceiver);
    }

    @Override
    public void updateReceiver(BizMerchantReceiver bizMerchantReceiver) {
        bizMerchantReceiverMapper.updateReceiver(bizMerchantReceiver);
    }

    @Override
    public Long selectMerchantReceiverMerchId(String idNumber) {
        return bizMerchantReceiverMapper.selectMerchantReceiverMerchId(idNumber);
    }
    @Override
    public BizMerchantReceiver selectReceiverMerchIdcard(String merchIdcard, Long merchId) {
        return bizMerchantReceiverMapper.selectReceiverMerchIdcard(merchIdcard,merchId);
    }

    @Override
    public String getStatus(String merchId) {
        return bizMerchantReceiverMapper.getStatus(merchId);
    }

    @Override
    public BizMerchantReceiver selectReceiverMerchIdcardIsSign(String merchIdcard, String dftype,String deptId) {
        return bizMerchantReceiverMapper.selectReceiverMerchIdcardIsSign(merchIdcard,dftype,deptId);
    }

    @Override
    public BizMerchantReceiver selectReceiverMerchIdcardIsSignLzf(String merchIdcard) {
        return bizMerchantReceiverMapper.selectReceiverMerchIdcardIsSignLzf(merchIdcard);
    }

    @Override
    public BizMerchantReceiver selectBizMerchantReceiverByUserIdAndMerchId(String merchId, String userId,String deptId) {
        return bizMerchantReceiverMapper.selectBizMerchantReceiverByUserIdAndMerchId(merchId,userId,deptId);
    }

    @Override
    public List<BizMerchantReceiver> getEachOne() {
        return bizMerchantReceiverMapper.getEachOne();
    }

    @Override
    public BizMerchantReceiver bizMerchantReceiver(BizMerchantReceiverDTO bizMerchantReceiverDTO) {
        return bizMerchantReceiverMapper.bizMerchantReceiver(bizMerchantReceiverDTO);
    }

}
